//
//  SpeechSynthesizer.swift
//  SpeechSynthesizer
//
//  Created by cqca202228560 on 2023/2/6.
//

import Foundation
import iflyMSC

open class SpeechSynthesizer: NSObject {
    
    public static let shared = SpeechSynthesizer.init()
    
    /// 是否正在播放
    public var isSpeaking: Bool {
        return self.synthesizer?.isSpeaking ?? false
    }
    
    fileprivate var synthesizer: IFlySpeechSynthesizer?
    
    private override init() {
        super.init()
        synthesizer = IFlySpeechSynthesizer.init()
        synthesizer?.delegate = self
        print(#file, #function)
    }
    
    /// 开始合成(播放)
    /// 调用此函数进行合成，如果发生错误会回调错误`onCompleted`
    /// - Parameter text: 合成的文本,最大的字节数为1k
    public func startSpeaking(text: String) {
        synthesizer?.startSpeaking(text)
    }
    
    /// 暂停播放
    /// 暂停播放之后，合成不会暂停，仍会继续，如果发生错误则会回调错误`onCompleted`
    public func pauseSpeaking() {
        synthesizer?.pauseSpeaking()
    }
    
    /// 恢复播放
    public func resumeSpeaking() {
        synthesizer?.resumeSpeaking()
    }
    
    /// 停止播放并停止合成
    public func stopSpeaking() {
        synthesizer?.stopSpeaking()
    }
}

extension SpeechSynthesizer: IFlySpeechSynthesizerDelegate {
    public func onCompleted(_ error: IFlySpeechError!) {
        guard let errorDesc = error.errorDesc else {
            return
        }
        print("onCompleted:", errorDesc)
    }
    
    
}
